﻿using System;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Windows.Forms;

using Wicresoft.Shell.Core;
using Wicresoft.Shell.Interfaces;

namespace Wicresoft.StartPage
{
    /// <summary>
    /// Summary description for MyControl.
    /// </summary>
    public partial class MyControl : UserControl
    {
        private ConnectInfos m_CnnInfos = null;

        public MyControl()
        {
            InitializeComponent();

            String path = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).FullName;
            int index = path.LastIndexOf("\\");
            path = path.Substring(0, index);
            var startPage = Path.Combine(path, "StartPage.htm");

            if (File.Exists(startPage))
            {
                webBrowser1.Navigate(startPage);
            }
        }

        /// <summary> 
        /// Let this control process the mnemonics.
        /// </summary>
        [UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
        protected override bool ProcessDialogChar(char charCode)
        {
            // If we're the top-level form or control, we need to do the mnemonic handling
            if (charCode != ' ' && ProcessMnemonic(charCode))
            {
                return true;
            }
            return base.ProcessDialogChar(charCode);
        }

        /// <summary>
        /// Enable the IME status handling for this control.
        /// </summary>
        protected override bool CanEnableIme
        {
            get
            {
                return true;
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {    
            var ul = webBrowser1.Document.GetElementById("ulRecentFiles");

            //ul.InnerHtml = "<li><a title=\"c:\\page1.umc\">page222.umc</a></li>";

            if (ul == null)
                return;
          
            m_CnnInfos = ConnectInfos.LoadConnectInfos();
            if (m_CnnInfos == null)
                return;

            int index = 0;
            foreach (ConnectInfoType eachCnnInfo in m_CnnInfos.ConnectInfo)
            {
                var li = webBrowser1.Document.CreateElement("li");
                li.SetAttribute("index", Convert.ToString(index++));
                li.SetAttribute("title", eachCnnInfo.ToString());
                li.Style = "cursor: hand";
                li.InnerText = new FileInfo(eachCnnInfo.ToString()).Name;
                li.Click += new HtmlElementEventHandler(li_Click);
                ul.AppendChild(li);
            }
        }

        void li_Click(object sender, HtmlElementEventArgs e)
        {
            var li = sender as HtmlElement;
            //OpenRecentFile(new RecentFileEventArgs(li.GetAttribute("title")));
            
            IConnectManagerService cnnManageService =
                Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SConnectManagerService)) as IConnectManagerService;
            if (null != cnnManageService)
            {
                int index = Convert.ToInt16(li.GetAttribute("index"));
                if (m_CnnInfos.ConnectInfo.Length > index)
                {
                    cnnManageService.ConnectToDB(m_CnnInfos.ConnectInfo[index]);
                    m_CnnInfos.SaveConnectInfos();
                }
            }
            else
            {
                MessageBox.Show("Cannot get GlobalService 'SConnectManageService'!");
            }

        }
    }
}
